{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d0267267-3893-49c3-9274-84f126f1d2ae",
   "metadata": {},
   "source": [
    "### <mark>题库<mark/>  \n",
    "智建2302 姚龙飞"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29170c58",
   "metadata": {},
   "source": [
    "我在题库基础上添加了登陆系统  \n",
    "账号：123 密码：321  \n",
    "如果登录系统无法正常使用，下方有单独拿出的优化后的题库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "83bfb545",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "菜单：\n",
      "1. 计算一年中的第几天\n",
      "2. 判断素数并计数\n",
      "3. 成绩等级\n",
      "4. 字符统计\n",
      "5. 完数查找\n",
      "6. 阶乘求和\n",
      "7. 打印菱形图案\n",
      "8. 数字排序\n",
      "9. 插入排序数组\n",
      "10. 约瑟夫环问题\n",
      "11. 显示帮助信息\n",
      "0. 退出程序\n",
      "*\n",
      "***\n",
      "*****\n",
      "*******\n",
      "*********\n",
      "*******\n",
      "*****\n",
      "***\n",
      "*\n",
      "\n",
      "菜单：\n",
      "1. 计算一年中的第几天\n",
      "2. 判断素数并计数\n",
      "3. 成绩等级\n",
      "4. 字符统计\n",
      "5. 完数查找\n",
      "6. 阶乘求和\n",
      "7. 打印菱形图案\n",
      "8. 数字排序\n",
      "9. 插入排序数组\n",
      "10. 约瑟夫环问题\n",
      "11. 显示帮助信息\n",
      "0. 退出程序\n",
      "感谢使用，再见！\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import tkinter as tk\n",
    "from tkinter import simpledialog\n",
    "from datetime import datetime\n",
    "\n",
    "# 使用JSON文件来存储数据\n",
    "DATA_FILE = 'data.json'\n",
    "\n",
    "# 加载数据\n",
    "def load_data():\n",
    "    try:\n",
    "        with open(DATA_FILE, 'r') as file:\n",
    "            return json.load(file)\n",
    "    except FileNotFoundError:\n",
    "        return {}\n",
    "\n",
    "# 保存数据\n",
    "def save_data(data):\n",
    "    with open(DATA_FILE, 'w') as file:\n",
    "        json.dump(data, file)\n",
    "\n",
    "# 注册用户\n",
    "def register_user(data):\n",
    "    username = simpledialog.askstring(\"注册\", \"请输入用户名：\")\n",
    "    password = simpledialog.askstring(\"注册\", \"请输入密码：\", show='*')\n",
    "    data['users'] = data.get('users', {})\n",
    "    data['users'][username] = password\n",
    "    save_data(data)\n",
    "\n",
    "# 登录用户\n",
    "def login_user(data):\n",
    "    username = simpledialog.askstring(\"登录\", \"请输入用户名：\")\n",
    "    password = simpledialog.askstring(\"登录\", \"请输入密码：\", show='*')\n",
    "    return data.get('users', {}).get(username) == password\n",
    "\n",
    "# 创建GUI\n",
    "def create_gui(data):\n",
    "    root = tk.Tk()\n",
    "    root.title(\"题库系统\")\n",
    "\n",
    "    def handle_login():\n",
    "        if login_user(data):\n",
    "            label.config(text=\"登录成功！\")\n",
    "            root.destroy()  # 关闭登录窗口\n",
    "            question_bank_menu()  # 打开题库菜单\n",
    "        else:\n",
    "            label.config(text=\"登录失败，请重试。\")\n",
    "\n",
    "    def handle_register():\n",
    "        register_user(data)\n",
    "        label.config(text=\"注册成功！\")\n",
    "\n",
    "    label = tk.Label(root, text=\"请选择登录或注册\")\n",
    "    label.pack()\n",
    "\n",
    "    login_button = tk.Button(root, text=\"登录\", command=handle_login)\n",
    "    login_button.pack()\n",
    "\n",
    "    register_button = tk.Button(root, text=\"注册\", command=handle_register)\n",
    "    register_button.pack()\n",
    "\n",
    "    root.mainloop()\n",
    "\n",
    "# 题库菜单\n",
    "def question_bank_menu():\n",
    "    while True:\n",
    "        print(\"\\n菜单：\")\n",
    "        print(\"1. 计算一年中的第几天\")\n",
    "        print(\"2. 判断素数并计数\")\n",
    "        print(\"3. 成绩等级\")\n",
    "        print(\"4. 字符统计\")\n",
    "        print(\"5. 完数查找\")\n",
    "        print(\"6. 阶乘求和\")\n",
    "        print(\"7. 打印菱形图案\")\n",
    "        print(\"8. 数字排序\")\n",
    "        print(\"9. 插入排序数组\")\n",
    "        print(\"10. 约瑟夫环问题\")\n",
    "        print(\"11. 显示帮助信息\")\n",
    "        print(\"0. 退出程序\")\n",
    "        choice = input(\"请输入选项：\")\n",
    "\n",
    "        if choice == '1':\n",
    "            year = int(input(\"请输入年份: \"))\n",
    "            month = int(input(\"请输入月份: \"))\n",
    "            day = int(input(\"请输入日期: \"))\n",
    "            print(f\"{year}-{month}-{day} 是这一年的第 {day_of_year(year, month, day)} 天。\")\n",
    "        elif choice == '2':\n",
    "            print(\"1000以内的素数有：\", [num for num in range(2, 1001) if is_prime(num)])\n",
    "        elif choice == '3':\n",
    "            score = float(input(\"请输入学生成绩：\"))\n",
    "            print(\"成绩等级：\", grade(score))\n",
    "        elif choice == '4':\n",
    "            input_str = input(\"请输入一行字符串：\")\n",
    "            counts = count_characters(input_str)\n",
    "            print(\"字符统计：\", counts)\n",
    "        elif choice == '5':\n",
    "            print(\"1000以内的完数有：\", find_perfect_numbers(1000))\n",
    "        elif choice == '6':\n",
    "            N = int(input(\"请输入任意数N：\"))\n",
    "            print(f\"1+2!+3!+…+{N}!的值为：{sum(factorial(i) for i in range(1, N+1))}\")\n",
    "        elif choice == '7':\n",
    "            N = int(input(\"请输入一个奇数N：\"))\n",
    "            print_diamond(N)\n",
    "        elif choice == '8':\n",
    "            print(\"排序后的数列为：\", sort_numbers())\n",
    "        elif choice == '9':\n",
    "            sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]\n",
    "            new_number = int(input(\"请输入一个数：\"))\n",
    "            print(\"插入新数后的数组为：\", insert_into_sorted_array(sorted_array, new_number))\n",
    "        elif choice == '10':\n",
    "            n = int(input(\"请输入人数n：\"))\n",
    "            print(\"最后留下的是原来第{}号的那位。\".format(last_remaining(n)))\n",
    "        elif choice == '11':\n",
    "            print(\"帮助信息：本程序提供多种数学和逻辑问题解决方案。\")\n",
    "        elif choice == '0':\n",
    "            print(\"感谢使用，再见！\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"无效的选项，请重新选择。\")\n",
    "\n",
    "# 定义其他函数\n",
    "def day_of_year(year, month, day):\n",
    "    return (datetime(year, month, day) - datetime(year, 1, 1)).days + 1\n",
    "\n",
    "def is_prime(n):\n",
    "    if n <= 1: return False\n",
    "    if n <= 3: return True\n",
    "    if n % 2 == 0 or n % 3 == 0: return False\n",
    "    i = 5\n",
    "    while i * i <= n:\n",
    "        if n % i == 0 or n % (i + 2) == 0: return False\n",
    "        i += 6\n",
    "    return True\n",
    "\n",
    "def grade(score):\n",
    "    if score >= 90: return 'A'\n",
    "    elif score >= 60: return 'B'\n",
    "    else: return 'C'\n",
    "\n",
    "def count_characters(input_str):\n",
    "    letter_count = sum(1 for char in input_str if char.isalpha())\n",
    "    space_count = sum(1 for char in input_str if char.isspace())\n",
    "    digit_count = sum(1 for char in input_str if char.isdigit())\n",
    "    other_count = len(input_str) - (letter_count + space_count + digit_count)\n",
    "    return letter_count, space_count, digit_count, other_count\n",
    "\n",
    "def find_perfect_numbers(limit):\n",
    "    perfect_numbers = [num for num in range(1, limit) if sum(i for i in range(1, num) if num % i == 0) == num]\n",
    "    return perfect_numbers\n",
    "\n",
    "def factorial(n):\n",
    "    return 1 if n == 0 else n * factorial(n-1)\n",
    "\n",
    "def print_diamond(N):\n",
    "    for i in range(1, N + 1, 2): print('*' * i)\n",
    "    for i in range(N - 2, 0, -2): print('*' * i)\n",
    "\n",
    "def sort_numbers():\n",
    "    numbers = [int(x) for x in input(\"请输入10个数，用空格分隔：\").split()]\n",
    "    numbers.sort()\n",
    "    return numbers\n",
    "\n",
    "def insert_into_sorted_array(sorted_array, new_number):\n",
    "    insert_position = next((i for i, x in enumerate(sorted_array) if x > new_number), len(sorted_array))\n",
    "    sorted_array.insert(insert_position, new_number)\n",
    "    return sorted_array\n",
    "\n",
    "def last_remaining(n):\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 2) % len(people)\n",
    "        people.pop(index)\n",
    "    return people[0]\n",
    "\n",
    "# 主函数\n",
    "def main():\n",
    "    data = load_data()\n",
    "    create_gui(data)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e65b343c",
   "metadata": {},
   "source": [
    "单独优化后题库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "20500812-748b-40aa-bb06-3bc69aa2aa35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "菜单：\n",
      "1. 计算一年中的第几天\n",
      "2. 判断素数并计数\n",
      "3. 成绩等级\n",
      "4. 字符统计\n",
      "5. 完数查找\n",
      "6. 阶乘求和\n",
      "7. 打印菱形图案\n",
      "8. 数字排序\n",
      "9. 插入排序数组\n",
      "10. 约瑟夫环问题\n",
      "11. 显示帮助信息\n",
      "0. 退出程序\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入选项： 1\n",
      "请输入年份:  2025\n",
      "请输入月份:  1\n",
      "请输入日期:  29\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-1-29 是这一年的第 29 天。\n",
      "\n",
      "菜单：\n",
      "1. 计算一年中的第几天\n",
      "2. 判断素数并计数\n",
      "3. 成绩等级\n",
      "4. 字符统计\n",
      "5. 完数查找\n",
      "6. 阶乘求和\n",
      "7. 打印菱形图案\n",
      "8. 数字排序\n",
      "9. 插入排序数组\n",
      "10. 约瑟夫环问题\n",
      "11. 显示帮助信息\n",
      "0. 退出程序\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入选项： 8\n",
      "请输入10个数，用空格分隔： 8 123 92 88 99 66\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后的数列为： [8, 66, 88, 92, 99, 123]\n",
      "\n",
      "菜单：\n",
      "1. 计算一年中的第几天\n",
      "2. 判断素数并计数\n",
      "3. 成绩等级\n",
      "4. 字符统计\n",
      "5. 完数查找\n",
      "6. 阶乘求和\n",
      "7. 打印菱形图案\n",
      "8. 数字排序\n",
      "9. 插入排序数组\n",
      "10. 约瑟夫环问题\n",
      "11. 显示帮助信息\n",
      "0. 退出程序\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入选项： 10\n",
      "请输入人数n： 5\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第4号的那位。\n",
      "\n",
      "菜单：\n",
      "1. 计算一年中的第几天\n",
      "2. 判断素数并计数\n",
      "3. 成绩等级\n",
      "4. 字符统计\n",
      "5. 完数查找\n",
      "6. 阶乘求和\n",
      "7. 打印菱形图案\n",
      "8. 数字排序\n",
      "9. 插入排序数组\n",
      "10. 约瑟夫环问题\n",
      "11. 显示帮助信息\n",
      "0. 退出程序\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入选项： 0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "感谢使用，再见！\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "def day_of_year(year, month, day):\n",
    "    return (datetime(year, month, day) - datetime(year, 1, 1)).days + 1\n",
    "\n",
    "def is_prime(n):\n",
    "    if n <= 1: return False\n",
    "    if n <= 3: return True\n",
    "    if n % 2 == 0 or n % 3 == 0: return False\n",
    "    i = 5\n",
    "    while i * i <= n:\n",
    "        if n % i == 0 or n % (i + 2) == 0: return False\n",
    "        i += 6\n",
    "    return True\n",
    "\n",
    "def grade(score):\n",
    "    if score >= 90: return 'A'\n",
    "    elif score >= 60: return 'B'\n",
    "    else: return 'C'\n",
    "\n",
    "def count_characters(input_str):\n",
    "    letter_count = sum(1 for char in input_str if char.isalpha())\n",
    "    space_count = sum(1 for char in input_str if char.isspace())\n",
    "    digit_count = sum(1 for char in input_str if char.isdigit())\n",
    "    other_count = len(input_str) - (letter_count + space_count + digit_count)\n",
    "    return letter_count, space_count, digit_count, other_count\n",
    "\n",
    "def find_perfect_numbers(limit):\n",
    "    perfect_numbers = [num for num in range(1, limit) if sum(i for i in range(1, num) if num % i == 0) == num]\n",
    "    return perfect_numbers\n",
    "\n",
    "def factorial(n):\n",
    "    return 1 if n == 0 else n * factorial(n-1)\n",
    "\n",
    "def print_diamond(N):\n",
    "    for i in range(1, N + 1, 2): print('*' * i)\n",
    "    for i in range(N - 2, 0, -2): print('*' * i)\n",
    "\n",
    "def sort_numbers():\n",
    "    numbers = [int(x) for x in input(\"请输入10个数，用空格分隔：\").split()]\n",
    "    numbers.sort()\n",
    "    return numbers\n",
    "\n",
    "def insert_into_sorted_array(sorted_array, new_number):\n",
    "    insert_position = next((i for i, x in enumerate(sorted_array) if x > new_number), len(sorted_array))\n",
    "    sorted_array.insert(insert_position, new_number)\n",
    "    return sorted_array\n",
    "\n",
    "def last_remaining(n):\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 2) % len(people)\n",
    "        people.pop(index)\n",
    "    return people[0]\n",
    "\n",
    "def main():\n",
    "    while True:\n",
    "        print(\"\\n菜单：\")\n",
    "        print(\"1. 计算一年中的第几天\")\n",
    "        print(\"2. 判断素数并计数\")\n",
    "        print(\"3. 成绩等级\")\n",
    "        print(\"4. 字符统计\")\n",
    "        print(\"5. 完数查找\")\n",
    "        print(\"6. 阶乘求和\")\n",
    "        print(\"7. 打印菱形图案\")\n",
    "        print(\"8. 数字排序\")\n",
    "        print(\"9. 插入排序数组\")\n",
    "        print(\"10. 约瑟夫环问题\")\n",
    "        print(\"11. 显示帮助信息\")\n",
    "        print(\"0. 退出程序\")\n",
    "        choice = input(\"请输入选项：\")\n",
    "\n",
    "        if choice == '1':\n",
    "            year = int(input(\"请输入年份: \"))\n",
    "            month = int(input(\"请输入月份: \"))\n",
    "            day = int(input(\"请输入日期: \"))\n",
    "            print(f\"{year}-{month}-{day} 是这一年的第 {day_of_year(year, month, day)} 天。\")\n",
    "        elif choice == '2':\n",
    "            print(\"1000以内的素数有：\", [num for num in range(2, 1001) if is_prime(num)])\n",
    "        elif choice == '3':\n",
    "            score = float(input(\"请输入学生成绩：\"))\n",
    "            print(\"成绩等级：\", grade(score))\n",
    "        elif choice == '4':\n",
    "            input_str = input(\"请输入一行字符串：\")\n",
    "            counts = count_characters(input_str)\n",
    "            print(\"字符统计：\", counts)\n",
    "        elif choice == '5':\n",
    "            print(\"1000以内的完数有：\", find_perfect_numbers(1000))\n",
    "        elif choice == '6':\n",
    "            N = int(input(\"请输入任意数N：\"))\n",
    "            print(f\"1+2!+3!+…+{N}!的值为：{sum(factorial(i) for i in range(1, N+1))}\")\n",
    "        elif choice == '7':\n",
    "            N = int(input(\"请输入一个奇数N：\"))\n",
    "            print_diamond(N)\n",
    "        elif choice == '8':\n",
    "            print(\"排序后的数列为：\", sort_numbers())\n",
    "        elif choice == '9':\n",
    "            sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]\n",
    "            new_number = int(input(\"请输入一个数：\"))\n",
    "            print(\"插入新数后的数组为：\", insert_into_sorted_array(sorted_array, new_number))\n",
    "        elif choice == '10':\n",
    "            n = int(input(\"请输入人数n：\"))\n",
    "            print(\"最后留下的是原来第{}号的那位。\".format(last_remaining(n)))\n",
    "        elif choice == '11':\n",
    "            print(\"帮助信息：本程序提供多种数学和逻辑问题解决方案。\")\n",
    "        elif choice == '0':\n",
    "            print(\"感谢使用，再见！\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"无效的选项，请重新选择。\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
